package com.example.actionbaractivity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.icu.text.DateFormat;
import java.text.SimpleDateFormat;

import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.telephony.SmsManager;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.widget.Toast;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.jar.Manifest;

public class ActionBarDemo extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_bar_demo);
        try{
            ViewConfiguration mconfig=ViewConfiguration.get(this);
            Field menuKeyField;
            menuKeyField=ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            if(menuKeyField!=null){
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(mconfig,false);
            }
        }catch(NoSuchFieldException e){
            e.printStackTrace();
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }catch (IllegalArgumentException e){
            e.printStackTrace();
        }
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
    @RequiresApi(api = Build.VERSION_CODES.M)
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.action_calendar:
                SimpleDateFormat formatter=new SimpleDateFormat("yy/MM/dd HH:mm:ss");
                Date curDate=new Date(System.currentTimeMillis());
                String str=formatter.format(curDate);
                Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_call:
                //6.0以上版本需要动态申请权限
                int hasPhoneCallPermission=checkSelfPermission(android.Manifest.permission.CALL_PHONE);
                if(hasPhoneCallPermission!= PackageManager.PERMISSION_GRANTED){
                    requestPermissions(new String[]{android.Manifest.permission.CALL_PHONE},123);
                }
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:10086"));
                startActivity(intent);
                break;
            case R.id.action_msm:
                int hasSmsPermission=checkSelfPermission(android.Manifest.permission.SEND_SMS);
                if(hasSmsPermission!= PackageManager.PERMISSION_GRANTED){
                    requestPermissions(new String[]{android.Manifest.permission.SEND_SMS},123);
                    break;
                }
                SmsManager smsM=SmsManager.getDefault();
                smsM.sendTextMessage("10086",null,"10086",null,null);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_action_bar_demo, menu);
        return true;
    }
}
